package com.blacksheep.mtm.controller;

import com.blacksheep.mtm.common.utils.ResultUtil;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.common.Result;
import com.blacksheep.mtm.pojo.manager.TbBoxDetails;
import com.blacksheep.mtm.service.BindBoxService;
import com.blacksheep.mtm.service.BoxRecycleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@RestController
@Api("冰包绑定记录")
public class BoxRecycleController {

    @Autowired
    private BoxRecycleService recycleService;

    @RequestMapping(value = "/box/recycle/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取温湿度计列表")
    public DataTablesResult getWSDeviceList(int draw, int start, int length, int bindId,
                                          @RequestParam("order[0][column]") int orderCol,
                                          @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","id", "box_code", "create_time","state"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }

        DataTablesResult result= recycleService.getList(draw,start,length,bindId,orderColumn,orderDir);
        return result;
    }

    @RequestMapping(value = "/box/recycle/add",method = RequestMethod.POST)
    @ApiOperation(value = "新增")
    public Result<Integer> insert(String boxCode,int bindId){
        int result = recycleService.add(bindId,boxCode);
        return new ResultUtil<Integer>().setData(result);
    }


    @RequestMapping(value = "/box/recycle/delete/{id}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除")
    public Result<Integer> delBind(@PathVariable Integer id){
        Integer result = recycleService.delete(id);
        return new ResultUtil<Integer>().setData(result);
    }


    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
